package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.GsonUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.CommonPhotoActivity;
import flc.ast.activity.MyAlbumActivity;
import flc.ast.activity.PhotoDetailActivity;
import flc.ast.activity.SelPicActivity;
import flc.ast.adapter.IdPhotoAdapter;
import flc.ast.bean.IdPhotosBean;
import flc.ast.databinding.FragmentIdPhotoBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.event.usersys.UserSysEventProxy;
import stark.common.basic.utils.IOUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link IdPhotoFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class IdPhotoFragment extends BaseNoModelFragment<FragmentIdPhotoBinding> {

    private IdPhotoAdapter idPhotoAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_id_photo;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.llCommonPhoto.setOnClickListener(this);
        mDataBinding.llProfessionalPhoto.setOnClickListener(this);
        mDataBinding.llCompositionPhoto.setOnClickListener(this);
        mDataBinding.tvPhotoType1.setOnClickListener(this);
        mDataBinding.tvPhotoType2.setOnClickListener(this);
        mDataBinding.tvPhotoType3.setOnClickListener(this);

        mDataBinding.rvIdPhotoList.setLayoutManager(new LinearLayoutManager(mContext));
        idPhotoAdapter = new IdPhotoAdapter();
        mDataBinding.rvIdPhotoList.setAdapter(idPhotoAdapter);
        idPhotoAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llCommonPhoto://常见寸照
                UserSysEventProxy.getInstance().payEvent(getActivity(), isRealOk -> {
                    if (isRealOk) {
                        startActivity(CommonPhotoActivity.class);
                    }
                });
                break;
            case R.id.llProfessionalPhoto://职业形象照
                UserSysEventProxy.getInstance().payEvent(getActivity(), isRealOk -> {
                    if (isRealOk) {
                        MyAlbumActivity.type = 1;
                        startActivity(MyAlbumActivity.class);
                    }
                });
                break;
            case R.id.llCompositionPhoto://电子排版
                UserSysEventProxy.getInstance().payEvent(getActivity(), isRealOk -> {
                    if (isRealOk) {
                        MyAlbumActivity.type = 2;
                        startActivity(MyAlbumActivity.class);
                    }
                });
                break;
            case R.id.tvPhotoType1://热门规格
                clearSelection();
                mDataBinding.tvPhotoType1.setTextSize(15);
                updateView(0);
                break;
            case R.id.tvPhotoType2://职业寸照
                clearSelection();
                mDataBinding.tvPhotoType2.setTextSize(15);
                updateView(1);
                break;
            case R.id.tvPhotoType3://考试报名
                clearSelection();
                mDataBinding.tvPhotoType3.setTextSize(15);
                updateView(2);
                break;

        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        UserSysEventProxy.getInstance().payEvent(getActivity(), isRealOk -> {
            if (isRealOk) {
                Content.sIdPhotosBean = idPhotoAdapter.getItem(position);
                startActivity(PhotoDetailActivity.class);
            }
        });
    }

    @Override
    protected void initData() {
        updateView(0);
    }

    private void updateView(int position) {
        try {
            InputStream is = mContext.getAssets().open("idPhoto" + position + ".json");
            String json = IOUtil.readStream2Str(is);
            List<IdPhotosBean> idPhotosBeans = GsonUtils.fromJson(json, new TypeToken<List<IdPhotosBean>>() {
            }.getType());
            idPhotoAdapter.setList(idPhotosBeans.subList(0, 3));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void clearSelection() {
        mDataBinding.tvPhotoType1.setTextSize(12);
        mDataBinding.tvPhotoType2.setTextSize(12);
        mDataBinding.tvPhotoType3.setTextSize(12);
    }

}